package org.itboys.mobile.util.SMS;

import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



/**
 * 大于短信
 * Created by weisky on 2016/11/19.
 */
public class DaYuSMSUtils {

    protected static Logger logger = LoggerFactory.getLogger(DaYuSMSUtils.class);

    private final static String SMS_URL = "http://gw.api.taobao.com/router/rest";//地址

    //这些信息项目自己配置
    private final static String APP_KEY = "23708474";
    private final static String SECRET = "84a3f4623ccca378deae7d7ccf34e2db";
    private final static String SIGN_NAME = "注册验证";
    private final static String TEMPLATE_CODE = "SMS_56625451";


    //	SMS_70565433 您当前的验证码是：${code}，操作类型：${operate}。请妥善保管，15分钟内有效。
    /**
     * 发送
     * @param extendId：透传ID 可为空
     * @param signName：签名
     * @param param：要替换的参数 例如：{"code":"1234","product":"修改密码"}
     * @param mobile：手机号
     * @param templateCode：模板ID ：SMS_25700432
     * @return
     */
   private static boolean dayuSendMSM(String extendId, String signName, String param, String mobile, String templateCode) {
       try {
           TaobaoClient client = new DefaultTaobaoClient(SMS_URL, APP_KEY, SECRET);
           AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
           req.setExtend(extendId);
           req.setSmsType("normal");
           req.setSmsFreeSignName(signName);
           req.setSmsParamString(param);
           req.setRecNum(mobile);
           req.setSmsTemplateCode(templateCode);
           AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
           // System.err.println("code"+rsp.getErrorCode()+",mesg."+rsp.getMsg());
           if (rsp.getResult().getSuccess()) {
               logger.info("短信发送成功，mobile=" + mobile);
               return true;
           } else {
               logger.info("短信发送失败，mobile=" + mobile);
               return false;
           }
       } catch (Exception e) {
           logger.error("短信发送失败,mobile=" + mobile, e);
           return false;
       }
   }

    public static void sendSMS(String mobile,String code,String operate){
        String param ="{\"code\":\"" + code +"\","+"\"product\":\"万品天街\"}";
        dayuSendMSM(null, SIGN_NAME, param, mobile,  TEMPLATE_CODE);
    }
    public static void main(String[] args) {

        sendSMS("18968173081","4356",null);
    }

}
